package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.RoutinePathologySamplingMapper;
import com.zhijian.medical.entity.po.RoutinePathologySample;
import com.zhijian.medical.entity.po.RoutinePathologySampling;
import com.zhijian.medical.entity.vo.request.RoutinePathologySamplingSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.RoutinePathologySamplingSearchVo;
import com.zhijian.medical.entity.vo.response.RoutinePathologySamplingVo;
import com.zhijian.medical.enums.RoutinePathologySampleStepEnum;
import com.zhijian.medical.service.RoutinePathologySampleService;
import com.zhijian.medical.service.RoutinePathologySamplingService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病理取材业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-06-10 22:25:01
 */
@Slf4j
@Service
public class RoutinePathologySamplingServiceImpl implements RoutinePathologySamplingService {

    @Autowired
    private RoutinePathologySamplingMapper mapper;

    @Autowired
    private RoutinePathologySampleService routinePathologySampleService;

    @Override
    public RoutinePathologySampling selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutinePathologySamplingVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutinePathologySampling routinePathologySampling) {
        Date now = new Date();
        routinePathologySampling.setCreateTime(now);
        routinePathologySampling.setUpdateTime(now);
        return mapper.insert(routinePathologySampling) > 0 ? routinePathologySampling.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutinePathologySampling routinePathologySampling) {
        Date now = new Date();
        routinePathologySampling.setCreateTime(now);
        routinePathologySampling.setUpdateTime(now);
        return mapper.insertSelective(routinePathologySampling) > 0 ? routinePathologySampling.getId() : 0L;
    }

    @Override
    public boolean update(RoutinePathologySampling routinePathologySampling) {
        Date now = new Date();
        routinePathologySampling.setUpdateTime(now);
        return mapper.updateByPrimaryKey(routinePathologySampling) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutinePathologySampling record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean save(RoutinePathologySamplingSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologySample routinePathologySample = routinePathologySampleService.selectById(saveOrUpdateVo.getSampleId());
        AssertUtil.notNull(routinePathologySample, "此样本不存在或者已被删除");
        AssertUtil.check(routinePathologySample.getStep() != RoutinePathologySampleStepEnum.QC.getCode().byteValue(), "此样本不允许取材");
        Integer count = this.count(RoutinePathologySamplingSearchVo.builder()
                .sampleId(saveOrUpdateVo.getSampleId())
                .build());
        AssertUtil.check(count > 0, "此样本不允许取材");
        RoutinePathologySampling routinePathologySampling = new RoutinePathologySampling();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologySampling);
        //把样本改成脱水状态
        routinePathologySampleService.updateByPrimaryKeySelective(RoutinePathologySample.builder()
                .id(saveOrUpdateVo.getSampleId())
                .step(RoutinePathologySampleStepEnum.TS.getCode().byteValue())
                .build());
        return this.insert(routinePathologySampling) != 0;
    }

    @Override
    public boolean modify(RoutinePathologySamplingSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologySampling old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutinePathologySampling routinePathologySampling = new RoutinePathologySampling();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologySampling);
        return this.update(routinePathologySampling);
    }

    @Override
    public RoutinePathologySamplingVo selectOne(RoutinePathologySamplingSearchVo routinePathologySamplingSearchVo) {
        return mapper.selectOne(routinePathologySamplingSearchVo);
    }

    @Override
    public Integer count(RoutinePathologySamplingSearchVo routinePathologySamplingSearchVo) {
        return mapper.count(routinePathologySamplingSearchVo);
    }

    @Override
    public List<RoutinePathologySamplingVo> selectList(RoutinePathologySamplingSearchVo routinePathologySamplingSearchVo) {
        return mapper.selectList(routinePathologySamplingSearchVo);
    }

    @Override
    public Page<RoutinePathologySamplingVo> selectPage(RoutinePathologySamplingSearchVo routinePathologySamplingSearchVo) {
        AssertUtil.check(Objects.isNull(routinePathologySamplingSearchVo.getPageNum()) || Objects.isNull(routinePathologySamplingSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routinePathologySamplingSearchVo);
        //分页
        Page<RoutinePathologySamplingVo> page = new Page<>(true, count, routinePathologySamplingSearchVo.getPageNum(), routinePathologySamplingSearchVo.getPageSize());
        routinePathologySamplingSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutinePathologySamplingVo> responseVos = this.selectList(routinePathologySamplingSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutinePathologySampling> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutinePathologySamplingSearchVo routinePathologySamplingSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routinePathologySamplingSearchVo);
    }

}